#include<homeworkhead.h>
#include"threadPool.h"
#include"head.h"
#include"taskQueue.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}

int OrderJudge(task_t * pTask){
    char buf[1024]={0};
    if(strncmp(pTask->buf,"cd ",3)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"cd",2)==0){
        cd(pTask);
    }
    else if(strncmp(pTask->buf,"ls ",3)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"ls",2)==0){
        ls(pTask);
    }
    else if(strncmp(pTask->buf,"puts ",5)==0){
       //cd(pTask);
       uploadFile(pTask);
    }
    else if(strncmp(pTask->buf,"gets ",5)==0){
        //puts("gets");
        downloadFile(pTask);
    }
    else if(strncmp(pTask->buf,"remove ",7)==0){
        //puts("remove");
        removeFile(pTask);
    }
    else if(strncmp(pTask->buf,"pwd",3)==0){
       printWorkingDir(pTask);
       //puts("pwd");
    }
    else if(strncmp(pTask->buf,"mkdir ",6)==0){
        myMkdir(pTask);
    }
    else if(strncmp(pTask->buf,"rmdir ",6)==0){
        //removeFile(pTask);
        puts("rmdir");
    }
    else{
        send(pTask->netFd,"命令错误，重新输入！",30,0);
        return -1;
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        task_t *pTask;//新建一个task来接收队首的任务
        pthread_mutex_lock(&pThreadPool->mutex);
        while (pThreadPool->taskQueue.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        handleEvent(pTask);
    }
}
int handleEvent(task_t* pTask){
    task_t mypTask = *pTask; //接收下来
    puts("welcome into handEvent!");
    int netFd = mypTask.netFd;
    //询问客户端登录或是注册
    int ret = send(netFd,"登录扣1注册扣2",22,0);
    ERROR_CHECK(ret,-1,"send0");
    char rebuf[20] = {0};
    ret = recv(netFd,rebuf,sizeof(rebuf),0);
    ERROR_CHECK(ret,-1,"recv0");
    char* replyNum = rebuf;
    
    //选择登录
    if(strcmp(replyNum,"1") == 0){
        puts("select 1");
        while(1){
            char userName[20] = {0};
            char password[20] = {0};
            ret = recv(netFd, userName, sizeof(userName), 0);
            ERROR_CHECK(ret,-1,"recv1");
            strcpy(mypTask.usrName ,userName);
            //puts(userName);
            ret = recv(netFd, password, sizeof(password), 0);
            ERROR_CHECK(ret,-1,"recv2");
            strcpy(mypTask.usePwd ,password);//task获得密码
            mysqlInit(&mypTask);//建立与数据库的连接
            //puts(password);
            ret = mysqlLogin(&mypTask);//登录验证
            if(ret == 0){
                send(netFd, "0", 2, 0);
                break;
            }else if(ret == -1){
                send(netFd, "-1", 3, 0);
                //循环再次输入
            }
        }
    }else if(strcmp(replyNum,"2") == 0){//选择注册
        mysqlInit(&mypTask);//建立与数据库的连接
        //注册用户进数据库
        ret = recv(netFd,mypTask.usrName,20,0);
        ERROR_CHECK(ret,-1,"recv usrname");
        //puts(mypTask.usrName);
        ret = recv(netFd,mypTask.usePwd,100,0);
        ERROR_CHECK(ret,-1,"recv usePwd");
        GenerateStr(mypTask.salt);//创建一个随机生成的八位数来作salt
        mysqlRegister(&mypTask);//用户连接test42这个数据库
        return 0;//注册完只能重新登录
    }else{
        send(netFd,"数字不对！",20,0);
        puts("client send error num,now is exit!");
        return -1;
    }

    while (1){
        bzero(mypTask.buf,sizeof(mypTask.buf));
        int ret = recv(mypTask.netFd,mypTask.buf,sizeof(mypTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        OrderJudge(&mypTask);
    }
    close(netFd);
    return 0;
}
